package com.cx.bank.factory;

import com.cx.bank.dao.BankDaoImpl;
import com.cx.bank.dao.BankDaoInterface;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * @author jxau_scf
 * @date 2021/7/24 - 10:22
 */
public class UserDaoFactory {
    private static UserDaoFactory userDaoFactory;
    private BankDaoInterface bankDao;

    public BankDaoInterface getBankDao() {
        return bankDao;
    }

    public void setBankDao(BankDaoInterface bankDao) {
        this.bankDao = bankDao;
    }

    private UserDaoFactory() throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException {
        File file = new File("src/main/resources/classInfo.properties");
        if (!file.exists()){
            System.out.println("classInfo.properties文件不存在");
        }
        Properties properties = new Properties();
        FileInputStream fis = new FileInputStream(file);
        properties.load(fis);
        String className = properties.getProperty("className");
        Class<?> clazz = Class.forName(className);
        bankDao = (BankDaoImpl)clazz.newInstance();
    }
    public static synchronized UserDaoFactory getInstance() throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {
        if (null != userDaoFactory) {
            return userDaoFactory;
        }
        userDaoFactory = new UserDaoFactory();
        return userDaoFactory;
    }
}
